<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.bpm.entity.businessTitle')">
              <a-input placeholder="" v-model="queryParam.businessTitle"></a-input>
            </a-form-item>
          </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item :label="$t('m.bpm.entity.processDefName')">
                <a-input placeholder="" v-model="queryParam.processDefName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item :label="$t('m.bpm.entity.processInstanceState')">
                <a-select v-model="queryParam.processInstanceState" placeholder="">
                  <a-select-option value="ALL">{{this.$t('m.common.label.all')}}</a-select-option>
                  <a-select-option v-for="item in processInstanceState" :key="item.value" :value="item.value">
                    {{ item.text }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
                <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              </span>
            </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        >
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('m.common.button.open') }}</a>
          <a-divider type="vertical" />
          <a @click="cancel(record)">{{ $t('m.common.button.cancleCollection') }}</a>
        </span>

      </a-table>
    </div>

    <redirect-modal ref="flowForm" @refreshParent="modalFormOk"></redirect-modal>
  </a-card>
</template>

<script>

import { YcListMixin } from '@/mixins/YcListMixin'
import { deleteAction,getAction } from '@/api/manage'
import { filterMultiDictText } from '@/components/dict/YchDictSelectUtil'
import RedirectModal from '../bpm/redirect/RedirectModal'
import { BpmConstant } from '../bpm/BpmConstant'
import YchConstant from '@/api/YchConstant'

export default {
  name: 'MyCollectionProcInstList',
  mixins: [YcListMixin],
  components: {
    RedirectModal,
  },
  props: {
    parentId:{
      type: String,
      default:null
    }
  },
  data() {
    return {
      processInstanceState: BpmConstant.processInstanceState,
      // 表头
      columns: [
        {
          title:this.$t('m.common.label.number'),
          dataIndex: '',
          key: 'rowIndex',
          width: '50px',
          align: 'center',
          scopedSlots: { customRender: 'serialNum' },
        },
        {
          title: this.$t('m.bpm.entity.businessTitle'),
          align: 'left',
          dataIndex: 'businessTitle',
          ellipsis: true,
        },
        {
          title: this.$t('m.bpm.entity.processDefName'),
          align: 'left',
          width: '130px',
          dataIndex: 'processDefName',
          ellipsis: true,
        },
        {
          title: this.$t('m.bpm.entity.startTimeString'),
          align: 'center',
          dataIndex: 'startTimeString',
          width: '130px'
        },
        {
          title: this.$t('m.bpm.entity.endTimeString'),
          align: 'center',
          dataIndex: 'endTimeString',
          width: '130px'
        },
        {
          title: this.$t('m.bpm.entity.processInstanceState'),
          align: 'center',
          dataIndex: 'processInstanceState',
          width: '80px',
          customRender: (text) => {
            if (!text) {
              return ''
            } else {
              return filterMultiDictText(BpmConstant.processInstanceState, text)
            }
          }
        },
        {
          title: this.$t('m.common.label.operate'),
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
          width: '120px'
        }
      ],
      url: {
        list: YchConstant.PREFIX_BPM + '/bpm-front/myProcessInstanceRest/getCollectProcInstIdListByParentId/v1',
        cancel: YchConstant.PREFIX_SYSTEM + "/collection/cancelByCatalogAndValue",
      }
    }
  },
  inject: ['addRouter'],
  methods: {
    searchReset() {
      this.queryParam = {
        collectionParentId: this.parentId
      }
      this.loadData(1)
    },
    loadData (arg) {
      if (!this.url.list) {
        this.messageUtil.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件
      if(this.parentId && this.parentId.length>0){
        params['collectionParentId'] = this.parentId;
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          }else {
            this.messageUtil.warning(res.message)
          }
          this.loading = false
        })
      }else{
        this.dataSource = []
      }


    },
    handleDetail(record) {
      let router = null
      if (this.$store.getters.formOpenType === '2') {
        router = this.addRouter({
          path: '/bpm/detailFromProcess/' + record.processInstanceId,
          component: resolve => require(['@/views/bpm/redirect/RedirectTab.vue'], resolve),
          props: {
            id: record.processInstanceId,
            func: 'detailFromProcess',
          },
          meta: {
            title: this.$t('m.bpm.common.bpmDetail'),
          }
        });
        if (router) {
          this.$router.push(router).catch(err => {})
        }
      }
      if (!router) {
        this.$refs.flowForm.detailFromProcess(record.processInstanceId)
      }
    },
    cancel(record){
      const that = this
      this.$confirm({
        title: this.$t('m.common.button.cancleCollection'),
        content: this.$t('m.common.tip.cancle_collection_confirm'),
        onOk: function () {
          that.spinning++
          deleteAction(that.url.cancel, { parentId: that.parentId,collectionType:'process',collectionValue:record.processInstanceId }).then((res) => {
            if (res.success) {
              that.messageUtil.success(res.message)
              that.loadData(1)
            } else {
              that.messageUtil.warning(res.message)
            }
          }).finally(() => {
            that.spinning--
          })
        }
      })
    },
  },
  watch: {
    parentId: {
      handler(value) {
        this.loadData(1)
      },
      immediate: true
    }
  }
}
</script>
<style lang="less" scoped>

</style>
